﻿using Microsoft.SharePoint.ApplicationPages.Calendar.RecurrenceRule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindStyle.SPCalendar.Pattern
{
    public class MonthlyByDayPattern : IPattern
    {
        public int MonthFrequency { get; set; }
        public Ordinal Ordinal { get; set; }
        public DaysOfWeek Day { get; set; }

        public MonthlyByDayPattern()
        {
            this.MonthFrequency = 1;
            this.Ordinal = Ordinal.First;
            this.Day = Utility.ParseDayOfWeek(DateTime.Today.DayOfWeek);
        }

        public string ToXML()
        {
            return string.Format("<monthlyByDay {0}=\"TRUE\" weekdayOfMonth=\"{1}\" monthFrequency=\"{2}\" />",
                    this.Day.ToSPString(),
                    this.Ordinal.ToString().ToLower(),
                    this.MonthFrequency);
        }



        public void FromRule(RecurrenceRule recurrence)
        {
            this.MonthFrequency = recurrence.Frequency;
            this.Ordinal = Utility.ParseOrdinal(recurrence.Ordinal);

            if (recurrence.IsDay)
                this.Day = SPCalendar.DaysOfWeek.Day;
            else if (recurrence.IsWeekday)
                this.Day = SPCalendar.DaysOfWeek.Weekday;
            else if (recurrence.IsWeekendDay)
                this.Day = SPCalendar.DaysOfWeek.WeekendDay;
            else if (recurrence.DaysOfWeek.Any())
                this.Day = Utility.ParseDayOfWeek(recurrence.DaysOfWeek.First());
        }
    }

}
